@extends('layouts.home')
@extends('layouts.nav')
@extends('layouts.top')

@section('css')
    <script type="text/javascript" src="{{ asset('vendor/jildertmiedema/laravel-plupload/js/plupload.full.min.js') }}"></script>
    <link rel="stylesheet" type="text/css" href="{{ asset('files/css/bootstrap-fileupload.min.css') }}" />
@endsection

@section('heading')
    <h3>
        课程设置
    </h3>
    <ul class="breadcrumb">
        <li>
            <a href="#">课程管理</a>
        </li>
        <li class="active"> 课程设置</li>
    </ul>
@endsection


@section('wrapper')
    <section class="panel">

        <div class="panel-body">
            <div class=" form">
                @if(isset($video))
                    <form method="POST" action="{{ route('video.update', $video->id) }}" class="cmxform form-horizontal adminex-form" enctype="multipart/form-data">
                        {{ method_field('PATCH') }}
                @else
                    <form method="POST" action="{{ route('video.store') }}" class="cmxform form-horizontal adminex-form" enctype="multipart/form-data">
                @endif
                    @csrf
                    <input value="{{ $video->video or old('video') }}" id="video" name="video" type="hidden"/>
                    <div class="form-group ">
                        <label for="cname" class="control-label col-lg-2">标题</label>
                        <div class="col-lg-5">
                            <input class=" form-control{{ $errors->has('title') ? ' error' : '' }}" id="cname" value="{{ $video->title or old('title') }}" name="title" minlength="2" type="text"/>
                            @if ($errors->has('title'))
                                <label class="error">{{ $errors->first('title') }}</label>
                            @endif
                        </div>
                    </div>
                    <div class="form-group last">
                        <label class="control-label col-md-2">封面</label>
                        <div class="col-md-9">
                            <div class="fileupload fileupload-new" data-provides="fileupload">
                                <div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
                                    <img src="{{ $video->imaging->path or '' }}" alt="" />
                                </div>
                                <div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
                                <div>
                                   <span class="btn btn-default btn-file" style="margin-left: 0px;">
                                       <span class="fileupload-new" style="margin-left: 0px;"><i class="fa fa-paper-clip"></i> 选择文件(尺寸:105*80px)</span>
                                       <span class="fileupload-exists" style="margin-left: 0px;"><i class="fa fa-undo"></i> 重选</span>
                                       <input type="file" name="image" class="default" />
                                   </span>
                                    <a href="#" class="btn btn-danger fileupload-exists" data-dismiss="fileupload" style="margin-left: 0px;"><i class="fa fa-trash"></i> 移除</a>
                                </div>
                            </div>
                            <br/>
                            @if ($errors->has('image'))
                                <label class="error">{{ $errors->first('image') }}</label>
                            @endif
                        </div>
                    </div>
                    <div class="form-group last">
                        <label class="control-label col-md-2">视频</label>
                        <div class="col-md-5">
                            <div class="fileupload fileupload-new" data-provides="fileupload">
                                <span id="videoContainer" class="btn btn-default btn-file" style="margin-left: 0px;">
                                    <span class="fileupload-new" id="uploadVideo" style="margin-left: 0px;"><i class="fa fa-paper-clip"></i> 选择文件</span>
                                </span>
                                @if (!empty($video->videoing))
                                    <label class="">{{ $video->videoing->name }}</label>
                                @endif
                                <span class="fileupload-preview" style="margin-left:5px;"></span>
                                <a href="#" class="close fileupload-exists" data-dismiss="fileupload" style="float: none; margin-left:5px;"></a>

                                <label class="" id="filelist"></label>
                                <div id="upload_progress" style="display: none;">
                                    <div class="progress progress-striped active progress-sm">
                                        <div id="progress" style="" aria-valuemax="100" aria-valuemin="0" aria-valuenow="45" role="progressbar" class="progress-bar progress-bar-success"></div>
                                    </div>
                                </div>
                            </div>
                            @if ($errors->has('video'))
                                <label class="error">{{ $errors->first('video') }}</label>
                            @endif
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label col-lg-2" for="inputSuccess">分类</label>
                        <div class="col-lg-5">
                            <select class="form-control m-bot15" name="category_id">
                                <option value="">选择</option>
                                @foreach($categorys as $category)
                                    @if(isset($video))
                                        <option {{ $video->category_id == $category->id ? 'selected' : ''}} value="{{ $category->id }}">{{ $category->name }}</option>
                                    @else
                                        <option {{ old('category_id') == $category->id ? 'selected' : ''}} value="{{ $category->id }}">{{ $category->name }}</option>
                                    @endif
                                @endforeach
                            </select>
                        </div>
                        @if ($errors->has('category_id'))
                            <label class="error">{{ $errors->first('category_id') }}</label>
                        @endif
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label col-lg-2" for="inputSuccess">教师</label>
                        <div class="col-lg-5">
                            <select class="form-control m-bot15" name="teacher_id">
                                <option value="">选择</option>
                                @foreach($teachers as $teacher)
                                    @if(isset($video))
                                        <option {{ $video->teacher_id == $teacher->id ? 'selected' : ''}} value="{{ $teacher->id }}">{{ $teacher->name }}</option>
                                    @else
                                        <option {{ old('teacher_id') == $teacher->id ? 'selected' : ''}} value="{{ $teacher->id }}">{{ $teacher->name }}</option>
                                    @endif
                                @endforeach
                            </select>
                        </div>
                        @if ($errors->has('teacher_id'))
                            <label class="error">{{ $errors->first('teacher_id') }}</label>
                        @endif
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label col-lg-2" >价格</label>
                        <div class="col-lg-5">
                            <input class=" form-control{{ $errors->has('price') ? ' error' : '' }}" value="{{ $video->price or old('price') }}" name="price" type="text"/>
                            @if ($errors->has('price'))
                                <label class="error">{{ $errors->first('price') }}</label>
                            @endif
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">简介</label>
                        <div class="col-sm-5">
                            <textarea rows="6" name="introduction" class="form-control">{{ $video->introduction or old('introduction') }}</textarea>
                        </div>
                        @if ($errors->has('introduction'))
                            <label class="error">{{ $errors->first('introduction') }}</label>
                        @endif
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">详情</label>
                        <div class="col-sm-10" id="div1">
                            @include('UEditor::head')
                            <script id="container" name="details" type="text/plain">
                                {!! $video->details or old('details') !!}
                            </script>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label col-lg-2" for="inputSuccess">教师</label>
                        <div class="col-lg-5">
                            <select class="form-control m-bot15" name="status">
                                <option value="">选择</option>
                                @if(isset($video))
                                <option {{ $video->status == 1 ? 'selected' : ''}} value="1">上架</option>
                                <option {{ $video->status == 0 ? 'selected' : ''}} value="0">下架</option>
                                    @else
                                    <option selected value="1">上架</option>
                                    <option value="0">下架</option>
                                @endif
                            </select>
                        </div>
                        @if ($errors->has('status'))
                            <label class="error">{{ $errors->first('status') }}</label>
                        @endif
                    </div>
                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-10">
                            <button class="btn btn-primary" type="submit">保存</button>
                        </div>
                    </div>
                </form>
            </div>

        </div>
    </section>
@endsection

@section('js')
    <script type="text/javascript">
        // Custom example logic

        var uploader = new plupload.Uploader({
            runtimes: 'html5,flash,silverlight,html4',
            browse_button: 'uploadVideo', // you can pass an id...
            container: document.getElementById('videoContainer'), // ... or DOM Element itself
            url: '{{ route('upload') }}',
            flash_swf_url: '{{ asset('vendor/jildertmiedema/laravel-plupload/js/Moxie.swf') }}',
            silverlight_xap_url: '{{ asset('vendor/jildertmiedema/laravel-plupload/js/Moxie.xap') }}',
            file_data_name: 'video',

            filters: {
                max_file_size: '200mb',
                mime_types: [
                    {title: "Video files", extensions: "mp4"},
                ],
                chunk_size: '4mb'
            },

            init: {
                PostInit: function () {
                    document.getElementById('filelist').innerHTML = '';
                },

                FilesAdded: function (up, files) {
                    plupload.each(files, function (file) {
                        uploader.start();
                        document.getElementById('filelist').innerHTML = '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
                        document.getElementById('upload_progress').style = 'display:block;'
                    });
                },

                UploadProgress: function (up, file) {
                    console.log(file.percent);
                    document.getElementById('progress').style = 'width: ' + file.percent + "%;";
                },

                Error: function (up, err) {
                    // document.getElementById('console').appendChild(document.createTextNode("\nError #" + err.code + ": " + err.message));
                },

                FileUploaded:function(up,file,result){
                    var jsonStr = JSON.parse(result.response);
                    var psel = document.getElementById("video");
                    psel.value = jsonStr.id
                }
            }
        });

        uploader.init();

    </script>
    <script type="text/javascript">
        var ue = UE.getEditor('container');
        ue.ready();
    </script>
    <script type="text/javascript" src="{{ asset('files/js/bootstrap-fileupload.min.js') }}"></script>
@endsection